package net.profo.infoplayas;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xml.sax.Attributes;
import org.xmlpull.v1.XmlPullParserException;

import android.sax.Element;
import android.sax.EndElementListener;
import android.sax.EndTextElementListener;
import android.sax.RootElement;
import android.sax.StartElementListener;
import android.util.Xml;

public class MunicipioParser 
{
	private Municipio municipioActual;
	private PlayaTools tools = new PlayaTools();
	private String titleReaded;
	
	public MunicipioParser()
	{
		
	}
	
	public List<Municipio> parse()
	{
		final List<Municipio> municipios = new ArrayList<Municipio>();
		
		RootElement root = new RootElement("Municipios");
		Element municipio = root.getChild("Municipio");
		
		municipio.setStartElementListener(new StartElementListener() {
			
			@Override
			public void start(Attributes attributes) 
			{
				municipioActual = new Municipio();
			}
		});
		
		municipio.setEndElementListener(new EndElementListener() {
			
			@Override
			public void end()
			{
				municipios.add(municipioActual);
			}
		});
		
		municipio.getChild("Pagina").setEndTextElementListener(new EndTextElementListener()
		{	
			@Override
			public void end(String body) 
			{
				municipioActual.setPagina(body);
			}
		});
		
		municipio.getChild("Nombre").setEndTextElementListener(new EndTextElementListener()
		{	
			@Override
			public void end(String body) 
			{
				municipioActual.setNombre(body);
			}
		});
		
		try
		{
			//Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
			Xml.parse(this.getInputStream(true),root.getContentHandler());
			//Xml.parse(this.getInputStream(true), new PlayaHandler());
		}
		catch(Exception ex)
		{
			throw new RuntimeException(ex);
		}
		
		return municipios;
	}
	
	private static final String METHOD_NAME = "ListaMunicipios";
	private static final String NAMESPACE = "http://tempuri.org/";
	private static final String SOAP_ACTION = "http://tempuri.org/ListaMunicipios";
	private static final String URL = "http://aspspider.info/PROFOTEAM/InfoPlayas.asmx";
	
	private String getInputStream(boolean b)
	{
		String res = "";
		
		try 
		{
			SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
			
			request.addProperty("provincia", "valencia");
			
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
			envelope.dotNet = true;
			envelope.bodyOut = request;
			envelope.setOutputSoapObject(request);
			
			HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
			androidHttpTransport.call(SOAP_ACTION, envelope);
			
			Object result = (Object)envelope.getResponse();
			
			res = result.toString();
			
		} 
		catch (SoapFault e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		catch (IOException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		catch (XmlPullParserException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return res;
	}
}
